package com.yzd.learnjava.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.SocketConfig;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;


public class HttpClientDemo {

	public static void main(String[] args) {
		new Thread(new Runnable() {
			
			public void run() {
				//useHttpCientGet("http://www.baidu.com");
				useClientPost("http://ip.taobao.com/service/getIpInfo.php");
			}
		}).start();
	}
	private static HttpClient createHttpClient() {
		//HttpParams defaltParams = new BasicHttpParams();
//		HttpConnectionParams.setConnectionTimeout(defaltParams, 1500);
		
		
		ConnectionConfig.Builder connBuilder = ConnectionConfig.custom();
		connBuilder.setCharset(Charset.forName("UTF-8"));
		ConnectionConfig connectionConfig = connBuilder.build();
		
		SocketConfig.Builder socketBuilder = SocketConfig.custom();
		socketBuilder.setSoTimeout(15000);
		socketBuilder.setSoKeepAlive(true);
		socketBuilder.setTcpNoDelay(true);
		SocketConfig socketConfig = socketBuilder.build();
		
		HttpClientBuilder clientBuilder = HttpClientBuilder.create();
		clientBuilder.setDefaultConnectionConfig(connectionConfig);
		clientBuilder.setDefaultSocketConfig(socketConfig);
		
		HttpClient client = clientBuilder.build();
		return client;
	}
	
	private static void useClientPost(String uri) {
		HttpPost post = new HttpPost(uri);
		post.addHeader("Connection", "Keep-Alive");
		try {
			HttpClient client = createHttpClient();
			
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("ip", "59.108.54.37"));
			post.setEntity(new UrlEncodedFormEntity(params));
			
			HttpResponse response = client.execute(post);
			
			HttpEntity entity = response.getEntity();
			int code = response.getStatusLine().getStatusCode();
			if(null != entity) {
				InputStream in = entity.getContent();
				String resp = converStreamToString(in);
				
				System.out.println("get, code : " + code + ", resp : " + resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
	}
	private static void useHttpCientGet(String url) {
		HttpGet get = new HttpGet(url);
		get.addHeader("Connection", "Keep-Alive");
		
		try {
			HttpClient client = createHttpClient();
			
			
			HttpResponse response = client.execute(get);
			HttpEntity entity = response.getEntity();
			int code = response.getStatusLine().getStatusCode();
			if(null != entity) {
				InputStream in = entity.getContent();
				String res = converStreamToString(in);
				System.out.println("get, code : " + code + ", resp : " + res);
				in.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private static String converStreamToString(InputStream in) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer sb = new StringBuffer();
		String line = null;
		while((line = reader.readLine()) != null) {
			sb.append(line);
			sb.append("\r\n");
		}
		return sb.toString();
	}
}
